﻿using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using ShellLink;
using System.Diagnostics;
using System.Collections;

namespace SetupCustomActions
{
    [RunInstaller(true)]
    public partial class CreateShortcuts : System.Configuration.Install.Installer
    {
        public CreateShortcuts()
        {
            InitializeComponent();
            Committed += new InstallEventHandler(this.CreateShortcutsCommitted);
            BeforeUninstall += new InstallEventHandler(CreateShortcutsBeforeUninstall);
            BeforeRollback += new InstallEventHandler(CreateShortcutsBeforeRallback);
        }

        public override void Install(IDictionary stateSaver)
        {
            Trace.WriteLine("On Install");

            base.Install(stateSaver);
        }

        protected override void OnBeforeUninstall(IDictionary savedState)
        {
            Trace.WriteLine("On Before Uninstall");

            base.OnBeforeUninstall(savedState);
        }

        protected override void OnBeforeRollback(IDictionary savedState)
        {
            Trace.WriteLine("On Before Rollback");

            base.OnBeforeRollback(savedState);
        }

        private void CreateShortcutsCommitted(object sender, InstallEventArgs e)
        {
            var shortcut1 = new ShellShortcut(
                DesktopFolderFile());

            SetShortCutValues(shortcut1);

            if (Directory.Exists(StartUserMenuFolder()))
            {
                var shortcut2 = new ShellShortcut(StartUserMenuFile());

                SetShortCutValues(shortcut2);
            }
        }

        private static string StartUserMenuFolder()
        {
            return
                Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),
                    @"Unreal Script IDE");
        }

        private static string StartUserMenuFile()
        {
            return
                Path.Combine(
                    StartUserMenuFolder(),
                    "Unreal Script IDE.lnk");
        }

        private static string DesktopFolderFile()
        {
            return 
                Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                    "Unreal Script IDE.lnk");
        }

        private void CreateShortcutsBeforeUninstall(object sender, InstallEventArgs e)
        {
            this.CreateShortcutsBeforeUninstallOrRallback(sender, e);
        }

        private void CreateShortcutsBeforeRallback(object sender, InstallEventArgs e)
        {
            this.CreateShortcutsBeforeUninstallOrRallback(sender, e);
        }

        private void CreateShortcutsBeforeUninstallOrRallback(object sender, InstallEventArgs e)
        {
            if (File.Exists(DesktopFolderFile()))
            {
                File.Delete(DesktopFolderFile());
            }

            if (File.Exists(StartUserMenuFile()))
            {
                File.Delete(StartUserMenuFile());
            }
        }

        private static void SetShortCutValues(ShellShortcut shortcut)
        {
            shortcut.Path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\UDKDevKitVSXShell.exe";
            shortcut.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            shortcut.Description = "Start Unreal Script IDE";
            shortcut.WindowStyle = ProcessWindowStyle.Normal;
            shortcut.Save();
        }
    }
}
